/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iftk.xmlparser.domain;

import java.util.*;
import org.jsoup.Jsoup;

/**
 *
 * @author treiman
 */
public class DailyMenu extends BubbleItem {
    
    private String date;
    private Calendar cal;
    
    private String menuContents;
    private List<String> meals;
    
    public DailyMenu() {
        meals = new ArrayList();
    }

    public String getMenuContents() {
        return menuContents;
    }

    public Calendar getCal() {
        return cal;
    }

    public List<String> getMeals() {
        return meals;
    }

    public void setMenuContents(String menuContents) {
        String[] tokens = menuContents.split("</p>");
        StringBuilder result = new StringBuilder();
        String NEW_LINE = System.getProperty("line.separator");

        for(int i = 0; i < tokens.length; i++) {
            meals.add(Jsoup.parse(tokens[i]).text());
            result.append(Jsoup.parse(tokens[i]).text());
            if(i != tokens.length-1) {
                result.append(NEW_LINE);
            }
        }
        
        this.menuContents = result.toString();
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        String newDate;
        this.date = date;
        cal = Calendar.getInstance();
        newDate = date.substring(date.length()-10, date.length());
        String[] tokens = newDate.split("\\p{Punct}");
        cal.set(Integer.parseInt(tokens[2]), Integer.parseInt(tokens[1])-1, Integer.parseInt(tokens[0]), 23, 59);
    }
    
    public boolean isInPast() {
        Calendar current = Calendar.getInstance();
        return (current.after(cal));
    }
    
    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        String NEW_LINE = System.getProperty("line.separator");
        
        result.append(this.getClass().getName()).append("Object {").append(NEW_LINE);
        result.append(" ID: ").append(getId()).append(NEW_LINE);
        result.append(" Date: ").append(date).append(NEW_LINE);
        result.append(" Menu: ").append(NEW_LINE);
        result.append(menuContents).append(NEW_LINE);        
        result.append(" Date is in the past: ").append(isInPast()).append(NEW_LINE);
        
        return result.toString();
    }
    
    @Override
    public String toBubble() {
        String dayOfWeek, dateToday;
        String[] tokens = date.split(" ");
        
        dayOfWeek = tokens[0];
        dateToday = tokens[1];
        
        Calendar calToday = Calendar.getInstance();
        
        StringBuilder result = new StringBuilder();
        String NEW_LINE = System.getProperty("line.separator");
        
        result.append("<bubble>").append(NEW_LINE);
        result.append("\t<id>").append(getId()).append("</id>").append(NEW_LINE);
        if(cal.get(Calendar.DAY_OF_YEAR) == calToday.get(Calendar.DAY_OF_YEAR) &&
                cal.get(Calendar.YEAR) == calToday.get(Calendar.YEAR)) {
            result.append("\t<titletext>Today</titletext>").append(NEW_LINE);
        }
        else {
            result.append("\t<titletext>").append(dayOfWeek).append(NEW_LINE).
                    append(dateToday).append("</titletext>").append(NEW_LINE);
        }
        result.append("\t<titlefontsize>10</titlefontsize>").append(NEW_LINE);
        result.append("\t<shorttext fontsize=\"3\">");
        for(int i = 0; i < meals.size(); i++) {
            if(i == meals.size()-1) {
                result.append(meals.get(i)).append("</shorttext>").append(NEW_LINE);
            }
            else {
                result.append(meals.get(i)).append(NEW_LINE).append(NEW_LINE);
            }
        }
        result.append("\t<style>PinkLevel2</style>").append(NEW_LINE);
        result.append("\t<links></links>").append(NEW_LINE);
        result.append("\t<priority>3</priority>").append(NEW_LINE);
        result.append("</bubble>").append(NEW_LINE);
        
        return result.toString();
    }
}
